package com.hmdp.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ShopTypeMapper shopTypeMapper;


    @Override
    public List<ShopType> getList() {
        // 从redis中查询店铺类型
        String listJson = stringRedisTemplate.opsForValue()
                .get("cache:nav:list");
        if (StrUtil.isNotBlank(listJson)) {
            // 如果有结果，则返回
            List<ShopType> shopTypes = JSONUtil.toList(listJson,ShopType.class);
            return shopTypes;
        }
        // 没有的话查询数据库
        List<ShopType> shopTypes = shopTypeMapper.selectList(null);
        // 将查询结果存入redis
        String jsonStr = JSONUtil.toJsonStr(shopTypes);
        stringRedisTemplate.opsForValue().set("cache:nav:list",jsonStr);
        // 返回结果
        return shopTypes;
    }
}
